from prg.header_info import HeaderInfo
from prg.proc.run_udp_thread import RunUdpBeat
from prg.bll.config_bll import ConfigBll
from prg.proc.udp_link_thread import UdpLink
from prg.common.object_message import MessageBase
from prg.common.import_file import *
import uuid


class RunReceiveSvr(MessageBase):
    def __init__(self, name="svr_receive", socket_item=None):
        super().__init__(name="run_receive_" + name)
        cfg = ConfigBll.instance()
        self.udp_queue = cfg.get_udp_port()
        self.verify = None
        self.svr_sign = None
        self.udp_port = None
        self.sign = None
        self.svr_address = None
        self.udp_svr = None
        self.link = None
        self.udp_receive = None
        self.udp_send = None
        self.svr = socket_item
        self.udp_linked = False
        self.link_id = None

    def run_listen(self, sign, udp_port, link="tcp"):
        self.sign = sign
        self.udp_port = udp_port
        self.link = link
        self.start()

    def entry(self):
        while self.is_run():
            head = HeaderInfo(client_socket=self.svr)
            self.svr_callback(head)

    def svr_callback(self, head):
        head_item = head.get_head_entity(key=self.sign)
        arg = head_item.arg
        if "udp" == head.get_command():
            print("callback udp server port: %s, udp port: %s" % (str(arg.arg_dict["port"]), str(self.udp_port)))
            self.svr_address = (arg.arg_dict["ip"], arg.arg_dict["port"])
            self.udp_svr = RunUdpBeat(name=head_item.name, udp_port=self.udp_port)
            self.udp_svr.bind_udp(address=self.svr_address, verify_code=self.verify, sign=self.sign)
            self.udp_linked = True
        elif "link" == head.get_command():
            print("callback link local port: %s, udp port: %s" % (str(self.udp_port), str(arg.arg_dict["port"])))
            udp_addr = (arg.arg_dict["ip"], arg.arg_dict["port"])
            link_port = int(arg.arg_dict["local"])
            task_id = arg.arg_dict["task"]
            udp_link = UdpLink(addr=udp_addr, link_port=link_port)
            udp_link.set_task_id(task_id)
            udp_link.set_group_name(str(uuid.uuid1()))
            udp_link.create_semaphore()
            PublicEntity.instance().get_task(task_id=task_id)
            udp_link.run_udp(udp_addr=self.udp_svr.udp_bind)
        elif "link-to" == head.get_command():
            print("callback link-to local port: %s, udp port: %s" % (str(self.udp_port), str(arg.arg_dict["port"])))
            udp_addr = (arg.arg_dict["ip"], arg.arg_dict["port"])
            link_port = int(arg.arg_dict["local"])
            task_id = arg.arg_dict["task"]
            udp_link = UdpLink(addr=udp_addr, link_port=link_port, link_type=2)
            udp_link.set_task_id(task_id)
            udp_link.set_group_name(str(uuid.uuid1()))
            udp_link.create_semaphore()
            PublicEntity.instance().get_task(task_id=task_id)
            udp_link.run_udp(udp_addr=self.udp_svr.udp_bind)
        elif "info" == head.get_command():
            callback_inf = (arg.arg_dict["link_id"], arg.arg_dict["name"], arg.arg_dict["verification"])
            print("server sign: %s" % arg.arg_dict["sign"])
            print("callback info id %s, name: %s, verification: %s" % callback_inf)
            self.verify = arg.arg_dict["verification"]
            self.svr_sign = arg.arg_dict["sign"]
            self.link_id = arg.arg_dict["link_id"]
        else:
            print("command not find")


